﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CosiNet.VM
{
    /// <summary>
    /// Class for providing methods for managing the call stack
    /// </summary>
    public static class CallStack
    {
        private static int[] memLocations = new int[255];
        private static int index = 0; 
        /// <summary>
        /// Pushes a bytecode location onto the call stack
        /// </summary>
        /// <param name="location">The location in code to add to the call stack</param>
        public static void Call(int location)
        {
            // Add the location to the top of the stack
            memLocations[index] = location;
            // Increment the stack index
            index += 1;
        }
        /// <summary>
        /// Pops and returns the last location from the call stack
        /// </summary>
        /// <returns>The location in the call stacks</returns>
        public static int Return()
        {
            // Get the location from the top of the stack
            int r = memLocations[index];
            // Overwrite the old location
            memLocations[index] = 0;
            // Decrement the stack index
            index -= 1;
            // Return the location we got earlier
            return r;
        }
    }
}
